package com.fjwt.gz.pay.payOrder;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.fjwt.gz.pay.PayKit;
import com.fjwt.gz.zhyx.SignUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Slf4j
@Service
public class CouponPayOrderService {

    /**
     * 同步卡券核销订单的文件
     **/
    public void getCouponPayOrder(JSONObject jsonObject) {
        log.info("同步卡券核销订单的文件未加密请求参数：{}", JSONUtil.toJsonStr(jsonObject));

        String realNameAuthUrl = PayKit.COUPON_PAY_ORDER_URL;
        long timeStamp = System.currentTimeMillis();
        Long reqTime = jsonObject.getLong("reqTime");
        String authSign = DigestUtils.md5DigestAsHex((PayKit.COUPON_PAY_ORDER_SECRET  + reqTime).getBytes());

        realNameAuthUrl = realNameAuthUrl + "?sign=" + authSign + "&reqTime=" + reqTime ;
        log.info("同步商户信息请求地址：{}, 请求参数：{}, 加密串：{}", realNameAuthUrl, authSign, authSign);
        try {

            HttpResponse response = HttpRequest.get(realNameAuthUrl)
                    .header("Content-Type", MediaType.TEXT_PLAIN_VALUE)
                    .header("sign-type", SignUtils.SIGN_TYPE)
                    .header("auth-apikey", SignUtils.API_KEY)
                    .header("auth-sign", authSign)
                    .header("auth-timestamp", String.valueOf(reqTime))
                    .header("Referer","https://jrstyy.post183.net")
                    .timeout(20000)
                    .execute();
            log.info("-----response----:{}", JSONUtil.toJsonStr(response));
            FileUtil.writeBytes(response.bodyBytes(),"D:\\sftp\\666\\666.csv");

        } catch (Exception e) {
            log.info(e.getMessage());

        }
    }
}
